﻿using HandyControl.Controls;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp1.Service;
using WpfApp1.Services;
using WpfApp1.ViewModel;
using System.Windows.Threading;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        private DispatcherTimer _timer;
        private string _displayText = "";
        private int _counter = 0;
        private static Dictionary<string, string> storeDic = new Dictionary<string, string>();
        public MainWindow()
        {
            InitializeComponent();
           // this.DataContext= App.Current.Services.GetService(typeof(MainViewModel));
          
        }

        //开启买家消息获取
        private void MessageForwarding()
        {
            string postUrl = "http://127.0.0.1:3030/SaiNiuApi/News";
            string postData = "&post=Start&data={}";
           // string result = HttpWebResponseUtility.PostDataGetHtml(postUrl, postData, "");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Growl.Success("显示一条通知！");
            GetStore();
        }
        /// <summary>
        /// 获取交互店铺
        /// </summary>
        private string GetStore()
        {
            storeDic = new Dictionary<string, string>();
            string postUrl = "http://127.0.0.1:3030/SaiNiuApi/Function";
            string postData = "&post=GetAllUser&data={}";
            string result = "";//HttpWebResponseUtility.PostDataGetHtml(postUrl, postData, "");
            dynamic data = JsonConvert.DeserializeObject<dynamic>(result);
            foreach (var key in data)
            {
                // 获取每个嵌套 JSON 字符串
                dynamic userData = JsonConvert.DeserializeObject<dynamic>(key.Value.ToString());
                // 提取 userNick 和 userUid
                string userNick = userData.userNick;
                string userUid = userData.userUid;
                storeDic.Add(userUid, userNick);

            }
            if (storeDic.Count > 0)
            {

                comboBox1.ItemsSource = storeDic;
            }
            else
            {
                //未获取到店铺
            }

            return result;
        }

        private void Pushmsg_Click(object sender, RoutedEventArgs e)
        {
            SendNewMsg();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            // 追加字符串 
            // _displayText += GetNewMsg();

            // 更新Label的显示内容 
            txtoutput.Text = _displayText;
            //Growl.Success("收到消息！");
        }
        static tbMessage tbmsg;
        /// 获取最新消息
        /// </summary>
        private string GetNewMsg()
        {
            string msg = "";

            string postUrl = "http://127.0.0.1:3030/SaiNiuApi/Call";
            string postData = "&post=GetNewNews&data={}";
            string result = "";//HttpWebResponseUtility.PostDataGetHtml(postUrl, postData, "");
            dynamic retsult = JsonConvert.DeserializeObject<dynamic>(result);
            if (retsult.code == 200)
            {
                var data = JObject.Parse(retsult.data.ToString());
                tbmsg = new tbMessage
                {
                    userNick = data.loginNick,
                    buyerNick = data.buyerNick,
                    buyerUid = data.buyerUid
                };
                return $"{data.message}\r\n";
            }
            if (retsult.code == 501)
            {
                _timer.Interval = TimeSpan.FromSeconds(5);
            }
            return "";

        }
        private string SendNewMsg()
        {

            string postUrl = "http://127.0.0.1:3030/SaiNiuApi/Function";
            string data = $"\"userNick\":\"{tbmsg.userNick}\",\"buyerNick\":\"{tbmsg.buyerNick}\",\"buyerUid\":\"{tbmsg.buyerUid}\",\"text\":\"{txtinput.Text}\",\"siteid\":\"cntaobao\",\"waitingTime\":3000";
            string postData = "&post=SendMessages&data={" + data + "}";
            string result =" HttpWebResponseUtility.PostDataGetHtml(postUrl, postData, ";
            dynamic retsult = JsonConvert.DeserializeObject<dynamic>(result);
            if (retsult.code == 200)
            {
                Growl.Success("回复成功！");

            }
            return "";

        }
    }
    public class tbMessage
    {
        public string message { get; set; }

        public string userNick { get; set; }
        public string buyerNick { get; set; }
        public string buyerUid { get; set; }
    }
}